package com.cy.pj.common.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/*在此切面对象中为目标对象做cache功能增强*/
@Aspect
@Component
public class SysCacheAspect {
    private Map<String,Object> cache=new ConcurrentHashMap<>();//线程安全的hashmap

    //@Pointcut("bean(*ServiceImpl)")
    //@Pointcut("execution(* com.cy.pj..*.findBrands(..))")//细粒度切入点
   @Pointcut("@annotation(com.cy.pj.common.annotation.RequiredCache)")
    public void doCache(){}

    //@Pointcut("execution(* com.cy.pj..*.deleteById(..))")//细粒度切入点
    @Pointcut("@annotation(com.cy.pj.common.annotation.ClearCache)")
    public void doClearCache(){}

    @AfterReturning("doClearCache()")
    public void doAfterReturning(){//在目标方法成功结束以后执行此方法
        cache.clear();
    }

/*    @Around("doClearCache()")
    public Object doClearAround(ProceedingJoinPoint jp)throws Throwable{
        Object obj=jp.proceed();
        cache.clear();
        return obj;
    }*/

    @Around("doCache()")
    public Object doCacheAround(ProceedingJoinPoint jp)throws Throwable{
        //从缓存取数据，假如缓存中有则直接返回，没有则查询数据库
        Object result=cache.get("cachekey");//暂时先将key设计为固定值
        if(result!=null)return result;
        //查询数据库
        result=jp.proceed();
        //将数据存储到缓存对象
        cache.put("cachekey", result);
        return result;
    }
}
